Assume the payload is going to be JSON

Daniel O'Connor 10 years ago
parent
commit
6a4779be9d
1 changed files with 23 additions and 1 deletions
  1. 23 1
      app/models/agents/mqtt_agent.rb

+ 23 - 1
app/models/agents/mqtt_agent.rb

@@ -1,5 +1,6 @@
1 1
 # encoding: utf-8 
2 2
 require "mqtt"
3
+require "json"
3 4
 
4 5
 module Agents
5 6
   class MqttAgent < Agent
@@ -56,6 +57,27 @@ module Agents
56 57
 
57 58
     MD
58 59
 
60
+    event_description <<-MD
61
+      Events are simply nested MQTT payloads. For example, an MQTT payload for Owntracks
62
+
63
+      <pre><code>{
64
+        "topic": "owntracks/kcqlmkgx/Dan",
65
+        "message": {
66
+  "topic": "owntracks/kcqlmkgx/Galaxy S3 Dan",
67
+  "message": {
68
+    "_type": "location",
69
+    "lat": "-34.849373",
70
+    "lon": "138.5218449",
71
+    "tst": "1401761484",
72
+    "acc": "10.0",
73
+    "batt": "71"
74
+  },
75
+  "time": 1401771825
76
+},
77
+        "time": 1401771051
78
+      }</code></pre>
79
+    MD
80
+
59 81
     def validate_options
60 82
       # unless options['uid'].present? &&
61 83
       #   options['expected_update_period_in_days'].present?
@@ -103,7 +125,7 @@ module Agents
103 125
     def check
104 126
       mqtt_client.connect do |c|
105 127
         c.get(options['topic']) do |topic,message|
106
-          create_event :payload => { 'topic' => topic, 'message' => message, 'time' => Time.now.to_i }
128
+          create_event :payload => { 'topic' => topic, 'message' => JSON.parse(message), 'time' => Time.now.to_i }
107 129
         end
108 130
       end
109 131
     end